
args loopnum acc trait
*** WWI Loop
use "Data/CivilWarIntemediateData.dta", clear
display "`acc'"
display "`loopnum'"
python:
from sfi import Data
import numpy as np
import pandas as pd
df = pd.DataFrame()
get_it = str("`trait'")
print(get_it)
X = np.array(Data.get("`trait'"))
df[get_it]  = X
acc2 = int(`acc')
rand_ind = df.sample(frac=(1 - acc2/100.0))
df['temp'] = df[get_it]
df.loc[rand_ind.index, 'temp'] += 1
df.loc[df.temp > 1, 'temp'] = 0
traitperturbed = df['temp'].values
Data.addVarByte('`trait'perturbed')
Data.store('`trait'perturbed', None, traitperturbed)
end


local trait "`trait'perturbed"   

preserve 

drop if year < 1905 | year > 1930

collapse (sum) patentcount (mean) `trait', by(nclassgoogle1 year)

tsset nclassgoogle1 year
tsfill, full

foreach var in `trait' patentcount {
	replace `var' = 0 if `var' == .
}

foreach var in `trait' {
	gen threeyear`var' = (`var'*patentcount + L.`var'*L.patentcount + L2.`var'*L2.patentcount)/(patentcount + L.patentcount + L2.patentcount)
}

foreach var in `trait' {
	gen fiveyear`var' = (`var'*patentcount + L.`var'*L.patentcount + L2.`var'*L2.patentcount + L3.`var'*L3.patentcount + L4.`var'*L4.patentcount)/(patentcount + L.patentcount + L2.patentcount + L3.patentcount + L4.patentcount)
}

foreach var in `trait' {
	gen fouryear`var' = (`var'*patentcount + L.`var'*L.patentcount + L2.`var'*L2.patentcount + L3.`var'*L3.patentcount)/(patentcount + L.patentcount + L2.patentcount + L3.patentcount)
}

foreach var in `trait' {
	gen sixyear`var' = (`var'*patentcount + L.`var'*L.patentcount + L2.`var'*L2.patentcount + L3.`var'*L3.patentcount + L4.`var'*L4.patentcount + L5.`var'*L5.patentcount)/(patentcount + L.patentcount + L2.patentcount + L3.patentcount + L4.patentcount + L5.patentcount)
}


drop if year < 1905

egen basepatent`trait'B = total(`trait') if year >= 1910 & year <= 1915, by(nclassgoogle1)
egen basepatent`trait' = max(basepatent`trait'B), by(nclassgoogle1)
drop if basepatent`trait' == 0

gen missingkeyvariable = fouryear`trait' == . if year > 1910
egen anymissing = max(missingkeyvariable), by(nclassgoogle1)
drop if anymissing == 1

gen L5fouryear`trait' = L5.fouryear`trait'
gen D5fouryear`trait' = fouryear`trait' - L5fouryear`trait'
drop if year < 1910

synth fouryear`trait' fouryear`trait'(1915) fouryear`trait'(1914) fouryear`trait'(1913) fouryear`trait'(1912) fouryear`trait'(1911) fouryear`trait'(1910), trunit(623) trperiod(1916) keep("Data/perturbation_results/SyntheticControlWWIperturb`trait'`acc'_`loopnum'.dta") replace


   

use "Data/perturbation_results/SyntheticControlWWIperturb`trait'`acc'_`loopnum'.dta", clear

rename _time year
rename _Y_synthetic `trait'control
rename _Y_treated `trait'treatment
tsset year 

gen period = .
replace period = 0 if year >= 1910 & year <= 1915
replace period = 1 if year >= 1916 & year <= 1922

egen treatmentperiod0B = mean(`trait'treatment) if period == 0
egen treatmentperiod0 = mean(treatmentperiod0B) 
egen treatmentperiod1B = mean(`trait'treatment) if period == 1
egen treatmentperiod1 = mean(treatmentperiod1B) 

egen controlperiod0B = mean(`trait'control) if period == 0
egen controlperiod0 = mean(controlperiod0B) 
egen controlperiod1B = mean(`trait'control) if period == 1
egen controlperiod1 = mean(controlperiod1B) 

gen diffindiff = (treatmentperiod1 - treatmentperiod0) - (controlperiod1 - controlperiod0)
keep if year == 1910
keep diffindiff
gen true = 0


save "Data/perturbation_results/SynthResultLeadingEdgeWWIperturb`trait'`acc'_`loopnum'.dta", replace
restore 



